package com.jiezhuo.controller.jzg;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.cmeduSchool.Jzg;
import com.jiezhuo.entity.cmeduSchool.JzgNewMenu;
import com.jiezhuo.entity.cmeduSchool.TJzg;
import com.jiezhuo.entity.jzg.*;
import com.jiezhuo.service.LoginService;
import com.jiezhuo.service.UserService;
import com.jiezhuo.service.jzg.JzgNewMenuService;
import com.jiezhuo.service.jzg.JzgUserlistService;
import com.jiezhuo.service.jzg.JzgYdService;
import com.jiezhuo.service.jzg.TJzgService;
import com.jiezhuo.utils.IDUtils;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import java.util.*;
import java.util.stream.Collectors;




/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2024-06-05
 */
@RestController
@Api(tags = "教职工")
@RequestMapping("/t-jzg")
public class TJzgController {
    @Autowired
    TJzgService tJzgService;
    @Autowired
    JzgNewMenuService jzgNewMenuService;

    @Autowired
    private LoginService loginService;
    @ApiOperation("获取在岗教师")
    @GetMapping("getOnGuardFaculty")
    public R getOnGuardFaculty( JzgVO jzgVO){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);

        if(ObjectUtils.isEmpty(jzgVO.getGzXqdm())){
            jzgVO.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jzgVO.getGzXqdm() + "'");
            if (!contains){
                jzgVO.setGzXqdm("'not'");
            }
        }
        List<HashMap<String,Object>> jzgList= tJzgService.getOnGuardFaculty(jzgVO);
        int num = tJzgService.getOnGuardFacultyCount(jzgVO);
        return R.ok().data("facultyInfo",jzgList).data("num",num);
    }
    @ApiOperation("获取详情数据")
    @GetMapping("getOnGuardFacultyInfo")
    public R getOnGuardFacultyInfo( JzgVO jzgVO){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        jzgVO.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        return tJzgService.getOnGuardFacultyInfo(jzgVO);
    }

    @ApiOperation("审查身份证")
    @PostMapping("examineCard")
    public R examineCard(@RequestBody TJzg tJzg){
        String sfzjh = tJzg.getSfzjh();
        boolean idNumber =true;
        if(tJzg.getSfzjlx().equals("1")){
            idNumber=IDUtils.isIDNumber(sfzjh);
        }
        boolean pd=false;
        if (idNumber && sfzjh.length()<=50 && !StringUtils.isEmpty(sfzjh)){
            pd=tJzgService.getCard(sfzjh);

        }else {
            return R.err().msg("请输入真确的证件号");
        }
        return  R.ok().data("examine",pd);
    }
    @ApiOperation("获取教职工初始数据")
    @GetMapping("getFacultyinitial")
    public R getFacultyinitial(TJzg tJzg){
        String sfzjh = tJzg.getSfzjh();
        boolean idNumber =true;
        if(tJzg.getSfzjlx().equals("1")){
            idNumber=IDUtils.isIDNumber(sfzjh);
        }
        HashMap<String ,Object> pd=null;
        if (idNumber && sfzjh.length()<=50 && !StringUtils.isEmpty(sfzjh)){
            pd= tJzgService.getCardInfo(sfzjh);

        }
        return  R.ok().data("faculty",pd);
    }
    @ApiOperation("新增教职工")
    @PostMapping("saveFaculty")
    public R saveFaculty(@RequestBody TJzg tJzg){

        String sfzjh = tJzg.getSfzjh();
        boolean idNumber =true;
        if(tJzg.getSfzjlx().equals("1")){
            idNumber=IDUtils.isIDNumber(sfzjh);
        }

        if (idNumber && sfzjh.length()<=50){
            return tJzgService.saveFaculty(tJzg);
        }else {
            return R.err().msg("请输入真确的证件号");
        }

    }


    @ApiOperation("修改教职工课程")
    @PostMapping("updateFaculty")
    public R updateFaculty(@RequestBody TJzg tJzg){
        ArrayList<String> teacherIds = tJzg.getTeacherIds();
        for (String teacherId : teacherIds) {
            QueryWrapper<TJzg> tJzgQueryWrapper = new QueryWrapper<>();
            tJzgQueryWrapper.eq("TeacherId",teacherId);

            TJzg one = tJzgService.getOne(tJzgQueryWrapper);
//            tJzg.setSfzjh(one.getSfzjh());
//            if (tJzg.getMqzjxk().equals("")){
//                tJzg.setMqzjxk(null);
//            }
            one.setMqzjxk(tJzg.getMqzjxk());
            tJzgService.getBaseMapper().update(one,tJzgQueryWrapper);
        }
        return R.ok();

    }


    @ApiOperation("修改教职工身份证号")
    @PostMapping("updateFacultyId")
    public R updateFacultyId(@RequestBody TJzg tJzg){
        String sfzjh = tJzg.getSfzjh();

        boolean idNumber =true;
        if(tJzg.getSfzjlx().equals("1")){
            idNumber=IDUtils.isIDNumber(sfzjh);
        }
        if (idNumber && sfzjh.length()<=50){

            boolean b = tJzgService.updateFacultyId(tJzg);
           return b?R.ok():R.err();
        }else {
            return R.err().msg("请输入真确的证件号");
        }

    }
    @Autowired
    JzgYdService jzgYdService;
    @ApiOperation("同意移动教职工")
    @PostMapping("moveFacultyConsent")
    public R moveFacultyConsent(@RequestBody JzgYdVO JzgYdVO){
        return tJzgService.moveFacultyConsent(JzgYdVO);
    }
    @ApiOperation("移动教职工")
    @PostMapping("moveFaculty")
    public R moveFaculty(@RequestBody TJzg jzg){
        return  tJzgService.moveFaculty(jzg);

    }

    @ApiOperation("移动教职工列表")
    @GetMapping("moveFacultyList")
    public R moveFacultyList( JzgYdVO jzgYdVO){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        String string = userPermissionInfo.getData().get("estate").toString();
        jzgYdVO.setJb(string);
        if(ObjectUtils.isEmpty(jzgYdVO.getGzXqdm())){
            jzgYdVO.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jzgYdVO.getGzXqdm() + "'");
            if (!contains){
                jzgYdVO.setGzXqdm("'not'");
            }
        }
        return jzgYdService.moveFacultyList(jzgYdVO);
    }
    @ApiOperation("校区移动教职工列表")
    @GetMapping("moveSchoolFacultyList")
    public R moveSchoolFacultyList( JzgYdVO jzgYdVO){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        String string = userPermissionInfo.getData().get("estate").toString();
        jzgYdVO.setJb(string);
        if(ObjectUtils.isEmpty(jzgYdVO.getGzXqdm())){
            jzgYdVO.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + jzgYdVO.getGzXqdm() + "'");
            if (!contains){
                jzgYdVO.setGzXqdm("'not'");
            }
        }
        return jzgYdService.moveSchoolFacultyList(jzgYdVO);
    }


    @ApiOperation("获取系统用户")
    @GetMapping("getUserList")
    public R getUserList(UserListVo userListVo){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        String string = userPermissionInfo.getData().get("estate").toString();

        if(ObjectUtils.isEmpty(userListVo.getGzXqdm())){
            userListVo.setGzXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + userListVo.getGzXqdm() + "'");
            if (!contains){
                userListVo.setGzXqdm("'not'");
            }
        }
        return tJzgService.getUserList(userListVo);
    }
    @Autowired
    JzgUserlistService jzgUserlistService;
    @ApiOperation("新增用户")
    @PostMapping("saveUserList")
    public R saveUserList(@RequestBody JzgUserlist jzgUserlist) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
        boolean contains = collect.contains("'" + jzgUserlist.getGlXqdm() + "'");
        if (!contains){
          return R.err().msg("无操作权限");
        }
        QueryWrapper<JzgUserlist> jzgUserlistQueryWrapper = new QueryWrapper<>();
        jzgUserlistQueryWrapper.orderByDesc("id");

        List<JzgUserlist> jzgUserlists = jzgUserlistService.getBaseMapper().selectList(jzgUserlistQueryWrapper);
        JzgUserlist jzgUserlist1 = jzgUserlists.get(0);
        jzgUserlistQueryWrapper.eq("UserId",jzgUserlist.getUserid());
        JzgUserlist one1 = jzgUserlistService.getOne(jzgUserlistQueryWrapper);
        if(!ObjectUtils.isEmpty(one1)){
            return  R.err().msg("用户已存在");
        }
        String string = userPermissionInfo.getData().get("estate").toString();
        if(string.equals("校级")){
            jzgUserlist.setJb("校级");
        }

        jzgUserlist.setId(jzgUserlist1.getId()+1);
        jzgUserlist.setRole("管理员");
        jzgUserlistService.getBaseMapper().insert(jzgUserlist);
        return  R.ok();
    }
    @ApiOperation("删除用户")
    @PostMapping("removeUerList")
    public R removeUerList(@RequestBody  JzgUserlist jzgUserlist) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        QueryWrapper<JzgUserlist> jzgUserlistQueryWrapper = new QueryWrapper<>();
        jzgUserlistQueryWrapper.eq("UserId",jzgUserlist.getUserid());
        String userId=loginUser.getUser().getUserid();
        JzgUserlist one = jzgUserlistService.getOne(jzgUserlistQueryWrapper);

        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
        boolean contains = collect.contains("'" + one.getGlXqdm() + "'");
        if (!contains){
            return R.err().msg("无操作权限");
        }

        int delete = jzgUserlistService.getBaseMapper().delete(jzgUserlistQueryWrapper);
        return  R.ok();
    }





}

